﻿using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using PmuRegistry.Silverlight.ModalDialogs;
using PmuRegistry.Silverlight.PmuRegistryDataServiceProxy;
using PmuRegistry.Silverlight.Utilities;

namespace PmuRegistry.Silverlight.Pages
{
	public partial class Login : Page
	{
		PhasorDataServiceClient m_client;
		ActivityWindow m_activityWindow;
		string userName;

		public Login()
		{
			InitializeComponent();
            this.Loaded += new RoutedEventHandler(Login_Loaded);
			m_client = Common.GetPhasorDataServiceClient();
			m_client.UserIsValidCompleted += new EventHandler<UserIsValidCompletedEventArgs>(m_client_UserIsValidCompleted);
			ButtonLogin.Click += new RoutedEventHandler(ButtonLogin_Click);			
		}

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            TextBoxUserName.Focus();
        }

		void m_client_UserIsValidCompleted(object sender, UserIsValidCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				if (e.Result)
				{
					App app = Application.Current as App;
					app.LoggedIn = true;
					app.UserName = userName;
					NavigationService.Navigate(new Uri("/Pages/QuickView.xaml", UriKind.Relative));
				}
				else
				{
					Message message = new Message();
					message.UserMessage = "Invalid User Name or Password";
					message.SystemMessage = "Failed to Authenticate User";
					message.UserMessageType = MessageType.Error;
					SystemMessages sm = new SystemMessages(message, ButtonType.OkOnly);
					sm.Show();
				}
			}
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Recently Added Devices", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
			if (m_activityWindow != null)
				m_activityWindow.Close();
		}

		void ButtonLogin_Click(object sender, RoutedEventArgs e)
		{
			Storyboard sb = new Storyboard();
			sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
			sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
			Storyboard.SetTarget(sb, ButtonLoginTransform);
			sb.Begin();

			m_activityWindow = new ActivityWindow("Authenticating... Please Wait...");
			m_activityWindow.Show();

			userName = TextBoxUserName.Text;
			string password = TextBoxPassword.Password;

			m_client.UserIsValidAsync(userName.ToLower(), password);

			//if ((userName.ToLower() == "pmuregistryuser" && password == "123-xyz") ||
			//    (userName.ToLower() == "kris.koellner@srpnet.com" && password == "srpnetuser") ||
			//    (userName.ToLower() == "tbilke@midwestiso.org" && password == "midwestisouser") ||
			//    (userName.ToLower() == "anthony.johnson@sce.com" && password == "sceuser") ||
			//    (userName.ToLower() == "jeff.dagle@pnl.gov" && password == "pnluser") ||
			//    (userName.ToLower() == "pmyrda@epri.com" && password == "epriuser") ||
			//    (userName.ToLower() == "david.burnham@ferc.gov" && password == "fercuser") ||
			//    (userName.ToLower() == "greg_zweigle@selinc.com" && password == "seluser") ||
			//    (userName.ToLower() == "hkhurana@illinois.edu" && password == "uiuserhk") ||
			//    (userName.ToLower() == "yardley@illinois.edu" && password == "uiuserty"))
			//{
			//    App app = Application.Current as App;
			//    app.LoggedIn = true;
			//    NavigationService.Navigate(new Uri("/Pages/QuickView.xaml", UriKind.Relative));
			//}
			//else
			//{
			//    Message message = new Message();
			//    message.UserMessage = "Invalid User Name or Password";
			//    message.SystemMessage = "Failed to Authenticate User";
			//    message.UserMessageType = MessageType.Error;
			//    SystemMessages sm = new SystemMessages(message, ButtonType.OkOnly);
			//    sm.Show();
			//}
		}

		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
		}

	}
}
